import cfg
import cnd
import tag

import datetime
import time

def job_real_time(jobname,odate):
	d=odate
	if cnd.exists(jobname+'.run',d) and cnd.exists(jobname+'.ok',d):
		start = cnd.time_mod(jobname+'.run',d)
		stop = cnd.time_mod(jobname+'.ok',d)
		return max(1,stop-start)
	else:
		return None

def job_real_times(jobname):
	times = {}
	for d in cnd.dates():
		t = job_real_time(jobname,d)
		if t:
			times[d] = t
	return times

def jobs_real_times(odate,tags=[]):
	d=odate
	times = {}
	if tags:
		tags = set(tags)
		jobs_with_tags = tag.jobs_with_tags(tags)
	for jobname in cnd.list(odate=d,type='ok',strip=True):
		if tags and (jobname not in jobs_with_tags):
			continue
		t = job_real_time(jobname,d)
		if t:
			times[jobname] = t
	return times



def job_start_time(jobname,odate):
	d=odate
	if cnd.exists(jobname+'.run',d):
		dti = cnd.time_mod(jobname+'.run',d)
		dt = datetime.datetime(*time.localtime(dti)[0:6])
		return dt.hour*3600+dt.minute*60+dt.second
	else:
		return None

def job_start_times(jobname):
	times = {}
	for d in cnd.dates():
		t = job_start_time(jobname,d)
		if t:
			times[d] = t
	return times

def jobs_start_times(odate,tags=[]):
	d=odate
	times = {}
	if tags:
		tags = set(tags)
		jobs_with_tags = tag.jobs_with_tags(tags)
	for jobname in cnd.list(odate=d,type='run',strip=True):
		if tags and (jobname not in jobs_with_tags):
			continue	
		t = job_start_time(jobname,d)
		if t:
			times[jobname] = t
	return times


def job_end_time(jobname,odate):
	d=odate
	if cnd.exists(jobname+'.ok',d) and cnd.exists(jobname+'.run',d):
		dti = cnd.time_mod(jobname+'.ok',d)
		dt = datetime.datetime(*time.localtime(dti)[0:6])
		return dt.hour*3600+dt.minute*60+dt.second
	else:
		return None

def job_end_times(jobname):
	times = {}
	for d in cnd.dates():
		t = job_end_time(jobname,d)
		if t:
			times[d] = t
	return times

def jobs_end_times(odate,tags=[]):
	d=odate
	times = {}
	if tags:
		tags = set(tags)
		jobs_with_tags = tag.jobs_with_tags(tags)
	for jobname in cnd.list(odate=d,type='ok',strip=True):
		if tags and (jobname not in jobs_with_tags):
			continue	
		t = job_end_time(jobname,d)
		if t:
			times[jobname] = t
	return times

def median(lst):
	if lst:
		return sorted(lst)[int(len(lst)/2)]
	else:
		return None

def med_real_time(jobname):
	return median(job_real_times(jobname).values())

def med_start_time(jobname):
	return median(job_start_times(jobname).values())

def med_end_time(jobname):
	return median(job_end_times(jobname).values())

def med_real_times(odate,tags=[]):
	d=odate
	times = {}
	if tags:
		tags = set(tags)
		jobs_with_tags = tag.jobs_with_tags(tags)
	for jobname in cnd.list(odate=d,type='ok',strip=True):
		if tags and (jobname not in jobs_with_tags):
			continue	
		t = med_real_time(jobname)
		if t:
			times[jobname] = t
	return times

if __name__=="__main__":
	cfg.cnd_root_dir=r"Y:\riv_prod\cnd"

